package cn.edu.ren.controller;

import cn.edu.ren.pojo.Student;
import cn.edu.ren.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @PackageName: cn.edu.ren.controller
 * @ClassName: StudentController
 * @Description:
 * @author: RZS
 * @date: 2021/5/19  9:54
 */
@Controller
@RequestMapping("/thymeleaf")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @GetMapping("/select_all")
    public ModelAndView selectStudentAll(){
        Collection<Student> stus = studentService.findAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("students", stus);
        return modelAndView;
    }

    @GetMapping("/thy")
    public String thymeleaf(){
        return "index";
    }

    @GetMapping("/text")
    public ModelAndView text(){
        ModelAndView model = new ModelAndView();
        model.setViewName("text");
        model.addObject("text", "Hello  Thymeleaf");
        return model;
    }

    @GetMapping("/utext")
    public ModelAndView utext(){
        ModelAndView model = new ModelAndView();
        model.setViewName("utext");
        model.addObject("utext", "<i>支持html的文本替换</i>");
        return model;
    }

    @GetMapping("/value")
    public ModelAndView value(){
        ModelAndView model = new ModelAndView();
        model.setViewName("value");
        model.addObject("value", "大锤");
        return model;
    }

    @GetMapping("/each")
    public ModelAndView each(){
        ModelAndView model = new ModelAndView();
        model.setViewName("each");

        List<String> list = new ArrayList<>();
        list.add("大锤");
        list.add("张三");
        list.add("李四");
        model.addObject("list", list);

        List<Student> lists = new ArrayList<>();
        lists.add(new Student(10, "大锤", 23));
        lists.add(new Student(15, "张三", 28));
        lists.add(new Student(8, "李四", 20));
        lists.add(new Student(5, "王五", 35));
        model.addObject("lists", lists);

        return model;
    }

    @GetMapping("/if_unless_switch")
    public ModelAndView ifUnlessSwitch(){
        ModelAndView model = new ModelAndView();
        model.setViewName("if-unless-switch");
        model.addObject("bool", "R");
        return model;
    }

    @GetMapping("/insert-include")
    public ModelAndView include(){
        ModelAndView model = new ModelAndView();
        model.setViewName("insert-replace-include");
        model.addObject("insert", "demo");
        return model;
    }

    @GetMapping("/object")
    public ModelAndView object(){
        ModelAndView model = new ModelAndView();
        model.setViewName("object");
        model.addObject("object", new Student(886, "大锤", 21));
        return model;
    }
}
